//
//  UIView+init.swift
//  ExtensionsKit
//
//  Created by TSYH on 2024/4/30.
//

import UIKit

public extension UIView {
    static var label: UILabel {
        return UILabel()
    }
    
    static var button: UIButton {
        return UIButton()
    }
    
    static var imageView: UIImageView {
        return UIImageView()
    }
    
    // MARK: 通用属性
    func background(_ color: UIColor?) -> Self {
        self.backgroundColor = color
        return self
    }
    
    func cornerRadius(_ value: CGFloat) -> Self {
        self.cornerRadius = value
        return self
    }
    
    func tintColor(_ color: UIColor) -> Self {
        self.tintColor = color
        return self
    }
    
    func alpha(_ value: CGFloat) -> Self {
        self.alpha = value
        return self
    }
    
    func frame(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) -> Self {
        self.frame = CGRect(x: 0, y: 0, width: width, height: height)
        return self
    }
    
    func size(width: CGFloat, height: CGFloat) -> Self {
        frame.size = CGSize(width: width, height: height)
        return self
    }
    
    @discardableResult
    func corner(_ radius: CGFloat, border width: CGFloat? = nil, color: UIColor? = nil) -> Self {
        if let w = width {
            self.borderWidth = w
        }
        if let color = color {
            self.borderColor = color
        }
        self.cornerRadius = radius
        return self
    }
}

public extension UILabel {
    convenience init(text: String?, font: UIFont, color: UIColor?) {
        self.init(frame: .zero)
        self.text = text
        self.font = font
        self.textColor = color
    }
    
    func text(_ value: String) -> UILabel {
        self.text = value
        return self
    }
    
    func font(_ size: CGFloat, weight: UIFont.Weight = .regular) -> UILabel {
        self.font = UIFont.systemFont(ofSize: size, weight: weight)
        return self
    }
    
    func font(_ font: UIFont) -> UILabel {
        self.font = font
        return self
    }
    
    func textColor(_ value: UIColor) -> UILabel {
        self.textColor = value
        return self
    }
    
    func lines(_ value: Int) -> UILabel {
        self.numberOfLines = value
        return self
    }
    
    func alignment(_ value: NSTextAlignment) -> UILabel {
        self.textAlignment = value
        return self
    }
}

public extension UIButton {
    convenience init(image: UIImage? = nil, 
                     title: String? = nil,
                     color: UIColor? = nil) {
        self.init(frame: .zero)
        self.setImage(image, for: .normal)
        self.setTitle(title, for: .normal)
        self.setTitleColor(color, for: .normal)
    }
    
    func title(_ value: String, state: UIControl.State = .normal) -> Self {
        self.setTitle(value, for: state)
        return self
    }
    
    func titleColor(_ value: UIColor?, state: UIControl.State = .normal) -> Self {
        self.setTitleColor(value, for: state)
        return self
    }
    
    func font(_ size: CGFloat, weight: UIFont.Weight = .regular) -> Self {
        self.titleLabel?.font = UIFont.systemFont(ofSize: size, weight: weight)
        return self
    }
    
    func font(_ font: UIFont) -> Self {
        self.titleLabel?.font = font
        return self
    }
    
    func disableHighlight() -> Self {
        self.showsTouchWhenHighlighted = false
        self.adjustsImageWhenHighlighted = false
        return self
    }
    
    func image(_ value: UIImage?, for state: UIControl.State = .normal) -> Self {
        self.setImage(value, for: state)
        return self
    }
    
    func add(_ target: Any?, action: Selector, for controlEvents: UIControl.Event = .touchUpInside) -> Self {
        addTarget(target, action: action, for: controlEvents)
        return self
    }
}

public extension UIImageView {
    func image(_ value: UIImage?) -> Self {
        self.image = value
        return self
    }
}
